<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(B卷,200分)- 观看文艺汇演问题（Java & JS & Python & C）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>为了庆祝中国共产党成立100周年&#xff0c;某公园将举行多场文艺表演&#xff0c;很多演出都是同时进行&#xff0c;一个人只能同时观看一场演出&#xff0c;且不能迟到早退&#xff0c;由于演出分布在不同的演出场地&#xff0c;所以连续观看的演出最少有15分钟的时间间隔&#xff0c;</p> 
<p>小明是一个狂热的文艺迷&#xff0c;想观看尽可能多的演出&#xff0c; 现给出演出时间表&#xff0c;请帮小明计算他最多能观看几场演出。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>第一行为一个数N&#xff0c;表示演出场数&#xff0c;1&lt;&#61;N&lt;&#61;1000&#xff0c;接下来N行&#xff0c;每行有被空格分割的两个整数&#xff0c;</p> 
<p>第一个整数T表示演出的开始时间&#xff0c;第二个整数L表示演出的持续时间&#xff0c;T和L的单位为分钟&#xff0c;0&lt;&#61;T&lt;&#61;1440,0&lt;L&lt;&#61;100.</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>输出最多能观看的演出场数。</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">2<br /> 720 120<br /> 840 120</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">1</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">两场演出间隔时间为0&#xff0c;不满足最小15分钟时间间隔的要求&#xff0c;所以最多只能观看一场演出</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">2<br /> 0 60<br /> 90 60</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">2</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">两场演出间隔大于15分钟&#xff0c;都能观看到</td></tr></tbody></table> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>本题其实是区间问题。且是要求解<span style="color:#fe2c24;"><strong>最多不相交区间个数</strong></span>。</p> 
<p>需要注意的是&#xff0c;两个区间不相交的条件也比较特殊&#xff1a;</p> 
<blockquote> 
 <p>连续观看的演出最少有15分钟的时间间隔</p> 
</blockquote> 
<p>即两个区间之间需要间隔15单位长度才算不相交。</p> 
<p>关于最多不相交区间个数的求解&#xff0c;请看&#xff1a;</p> 
<p><a href="https://blog.csdn.net/qfc_128220/article/details/128386257?spm&#61;1001.2014.3001.5501" title="华为OD机试 - 最多等和不相交连续子序列_伏城之外的博客-CSDN博客">华为OD机试 - 最多等和不相交连续子序列_伏城之外的博客-CSDN博客</a></p> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JavaScript算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

const lines &#61; [];
let n;
rl.on(&#34;line&#34;, (line) &#61;&gt; {
  lines.push(line);

  if (lines.length &#61;&#61;&#61; 1) {
    n &#61; lines[0] - 0;
  }

  if (n &amp;&amp; lines.length &#61;&#61;&#61; n &#43; 1) {
    lines.shift();
    const matrix &#61; lines.map((line) &#61;&gt; line.split(&#34; &#34;).map(Number));
    console.log(getResult(matrix));
    lines.length &#61; 0;
  }
});

function getResult(matrix) {
  matrix &#61; matrix
    .map(([start, spend]) &#61;&gt; [start, start &#43; spend])
    .sort((a, b) &#61;&gt; a[1] - b[1]);

  let t &#61; matrix[0][1];
  let ans &#61; 1;
  for (let i &#61; 1; i &lt; matrix.length; i&#43;&#43;) {
    const [l, r] &#61; matrix[i];

    if (l - t &gt;&#61; 15) {
      ans&#43;&#43;;
      t &#61; r;
    }
  }

  return ans;
}
</code></pre> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.Arrays;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    int n &#61; sc.nextInt();

    int[][] ranges &#61; new int[n][2];
    for (int i &#61; 0; i &lt; n; i&#43;&#43;) {
      ranges[i][0] &#61; sc.nextInt(); // 开始时间
      ranges[i][1] &#61; ranges[i][0] &#43; sc.nextInt(); // 结束时间 &#61; 开始时间 &#43; 持续时间
    }

    System.out.println(getResult(ranges));
  }

  public static int getResult(int[][] ranges) {
    Arrays.sort(ranges, (a, b) -&gt; a[1] - b[1]);

    int t &#61; ranges[0][1];
    int ans &#61; 1;

    for (int i &#61; 1; i &lt; ranges.length; i&#43;&#43;) {
      int l &#61; ranges[i][0];
      int r &#61; ranges[i][1];

      if (l - t &gt;&#61; 15) {
        ans&#43;&#43;;
        t &#61; r;
      }
    }

    return ans;
  }
}
</code></pre> 
<p></p> 
<h4 style="background-color:transparent;">Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
n &#61; int(input())
tmp &#61; [list(map(int, input().split())) for _ in range(n)]
rans &#61; list(map(lambda ran: [ran[0], ran[0] &#43; ran[1]], tmp))


# 算法入口
def getResult():
    rans.sort(key&#61;lambda ran: ran[1])

    t &#61; rans[0][1]
    ans &#61; 1

    for i in range(1, len(rans)):
        l, r &#61; rans[i]
        if l - t &gt;&#61; 15:
            ans &#43;&#61; 1
            t &#61; r

    return ans


# 算法调用
print(getResult())
</code></pre> 
<p></p> 
<h4>C算法源码</h4> 
<pre><code class="language-cpp">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

int cmp(const void* a, const void* b) {
    return ((int*) a)[1] - ((int*) b)[1];
}

int main() {
    int n;
    scanf(&#34;%d&#34;, &amp;n);

    int ranges[n][2];
    for(int i&#61;0; i&lt;n; i&#43;&#43;) {
        scanf(&#34;%d %d&#34;, &amp;ranges[i][0], &amp;ranges[i][1]);
        ranges[i][1] &#43;&#61; ranges[i][0];
    }

    qsort(ranges, n, sizeof(int*), cmp);

    int t &#61; ranges[0][1];
    int ans &#61; 1;

    for(int i&#61;1; i&lt;n; i&#43;&#43;) {
        int l &#61; ranges[i][0];
        int r &#61; ranges[i][1];

        if(l - t &gt;&#61; 15) {
            ans&#43;&#43;;
            t &#61; r;
        }
    }

    printf(&#34;%d\n&#34;, ans);

    return 0;
}</code></pre> 
<p> </p>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>